ALTER proc sp_change_quotation_proc
(
	@Q_Customer_PO_Number 		varchar(50),
	@Q_Contact_Person 		varchar(50),
	@Q_Sales_Person 		varchar(50),
	@Q_Shipping_Method 		varchar(50),
	@Q_Shipping_Account 		varchar(50),
	@Q_Billing_Address_ID 		int,	
	@Q_Shipping_Address_ID 		int,
	@Q_Comments 			varchar(500),
	@Q_Shipping_Instructions 	varchar(500),
	@Q_EndUser 			varchar(50),
	@Q_Sales_Tax_Type 		varchar(50),
	@Q_Sub_Total 			float,
	@Q_Sales_Tax 			float,
	@Q_Frieght_Tax 			float,
	@Q_Misc 			float,
	@Quotation_No 			varchar(50),
	@Regional_Rep 			varchar(50),
	@Shipping_Terms 		varchar(50),
	@Delivery_Terms 		varchar(50),
	@Q_Booking_date 		datetime,
	@Quotation_id			int,
	@CSO_Reason			varchar(50),
	@Phone				varchar(50),
	@Fax				varchar(50),
	@Email				varchar(50),
	@miscName			varchar(30),
	@Expiry_Date 		        datetime,
	@Q_Billing_Contact_Person 	varchar(50), 
	@Q_Shipping_Contact_Person 	varchar(50),
	@CellNumber  			varchar(50),
	@Q_Sales_Rep 			varchar(50), 
	@Q_PO_Rev 			varchar(50),
	@Q_PO_Date 			datetime,
	@Q_ODM_ID 			int,
	@Q_Is_Customer_Ship_Address 	bit,
	@Q_Freight_Instructions 	varchar(500),   
	@Q_SHIPPING_INSTRUCTIONS_ID 	INT,
	@Q_Freight_Instructions_Id 	INT,
	@Q_ENDCUSTOMER_ID		INT,
	@Q_QUOTE_TYPE			VARCHAR(50),
	@Q_APPLICATION			VARCHAR(50),
	@Q_ADD_NEW_APPLICATION		VARCHAR(50),
 	@Q_NEW_END_USER 		VARCHAR(50),
	@Q_NEW_END_CUSTOMER		VARCHAR(50),
	@Q_EXPECTED_PO_DATE		datetime,
	@Q_PROJECT_NAME		VARCHAR(100),
	@Q_OPPORTUNITY_ID	VARCHAR(100)
)
AS
	declare @Q_Customer_ID int
	begin
		UPDATE QUOTATION_TABLE SET 
		Q_Customer_PO_Number = @Q_Customer_PO_Number,
		Q_Contact_Person =@Q_Contact_Person,
		Q_Sales_Person = @Q_Sales_Person,
		Q_Shipping_Method = @Q_Shipping_Method,
		Q_Shipping_Account = @Q_Shipping_Account,
		Q_Billing_Address_ID = @Q_Billing_Address_ID,
		Q_Shipping_Address_ID = @Q_Shipping_Address_ID,
		Q_Comments = @Q_Comments,
		Q_Shipping_Instructions = @Q_Shipping_Instructions,
		Q_EndUser = @Q_EndUser,
		Q_Sales_Tax_Type = @Q_Sales_Tax_Type,
		Q_Sub_Total = @Q_Sub_Total,
		Q_Sales_Tax = @Q_Sales_Tax,
		Q_Frieght_Tax = @Q_Frieght_Tax,
		Q_Misc = @Q_Misc,
		Quotation_No = @Quotation_No,
		Regional_Rep = @Regional_Rep,
		Shipping_Terms = @Shipping_Terms,
		Delivery_Terms = @Delivery_Terms,
		Q_Booking_date = @Q_Booking_date,
		Q_Misc_Name = @miscName,
		Expiry_Date = @Expiry_Date,
		Q_Billing_Contact_Person = @Q_Billing_Contact_Person, 	
		Q_Shipping_Contact_Person = @Q_Shipping_Contact_Person,
		timestamp = CURRENT_TIMESTAMP,
		Q_Sales_Rep = @Q_Sales_Rep,
		Q_PO_Rev = @Q_PO_Rev,
		Q_PO_Date = @Q_PO_Date,  
		Q_ODM_ID  = @Q_ODM_ID,
		Q_Is_Customer_Ship_Address = @Q_Is_Customer_Ship_Address,
		Q_Freight_Instructions = @Q_Freight_Instructions, 
		Q_SHIPPING_INSTRUCTIONS_ID = @Q_SHIPPING_INSTRUCTIONS_ID,
		Q_Freight_Instructions_Id = @Q_Freight_Instructions_Id,
		Q_ENDCUSTOMER_ID = @Q_ENDCUSTOMER_ID,
		Q_QUOTE_TYPE = @Q_QUOTE_TYPE,
		Q_APPLICATION = @Q_APPLICATION,
		Q_ADD_NEW_APPLICATION = LTRIM(RTRIM(@Q_ADD_NEW_APPLICATION)),
		Q_NEW_END_USER	      = LTRIM(RTRIM(@Q_NEW_END_USER)), 		
		Q_NEW_END_CUSTOMER    = LTRIM(RTRIM(@Q_NEW_END_CUSTOMER)),	 	
		Q_EXPECTED_PO_DATE    = @Q_EXPECTED_PO_DATE,
		Q_PROJECT_NAME = @Q_PROJECT_NAME,
		Q_OPPORTUNITY_ID = @Q_OPPORTUNITY_ID
		WHERE Quotation_ID = @Quotation_id      
	end
	
	set @Q_Customer_ID = (SELECT Customer_ID FROM QUOTATION_TABLE WHERE Quotation_ID = @Quotation_id )
	if( @Q_Contact_Person = ( select C_Name FROM Customer WHERE Customer_Id = @Q_Customer_ID ))
	begin
		declare @Temp_Id int
		SELECT @Temp_Id = count(Quotation_Id)FROM Quotation_Address WHERE  Quotation_ID = @Quotation_id 
		if ( @temp_Id = 0 )
			begin
				INSERT INTO Quotation_Address (Phone_Number,Fax_Number,Email,Quotation_ID) values(@Phone,@Fax,@Email,@Quotation_id) 
			end
		else				
			begin				
				UPDATE Quotation_Address SET Phone_Number = @Phone, Fax_Number = @Fax , Email = @Email 
				WHERE  Quotation_ID = @Quotation_id 
			end
	end
	else
	begin
		UPDATE Customer_Contact_Persons_Table  SET Phone_Number = @Phone, Fax= @Fax , Email = @Email , Cell_Number =  @CellNumber
		WHERE  Customer_Id = @Q_Customer_ID
                and Contact_person = @Q_Contact_Person	
	end


